/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.library.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import lombok.SneakyThrows;
import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.library.entity.MaterialLibraryEntity;
import org.springblade.modules.library.service.IMaterialLibraryService;
import org.springblade.modules.personal.entity.SecrecyLevel;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.enums.AttachType;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.library.entity.CarouselImgEntity;
import org.springblade.modules.library.vo.CarouselImgVO;
import org.springblade.modules.library.excel.CarouselImgExcel;
import org.springblade.modules.library.wrapper.CarouselImgWrapper;
import org.springblade.modules.library.service.ICarouselImgService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 首页轮播图表 控制器
 *
 * @author junxin
 * @since 2023-05-16
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-carouselImg/carouselImg")
@Api(value = "首页轮播图表", tags = "首页轮播图表接口")
public class CarouselImgController extends BladeController {

	private final ICarouselImgService carouselImgService;
	private final OssBuilder ossBuilder;
	private final IMaterialLibraryService materialLibraryService;


	/**
	 * 首页轮播图表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入carouselImg")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R<CarouselImgVO> detail(CarouselImgEntity carouselImg) {
		CarouselImgEntity detail = carouselImgService.getOne(Condition.getQueryWrapper(carouselImg));
		return R.data(CarouselImgWrapper.build().entityVO(detail));
	}
	/**
	 * 首页轮播图表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入carouselImg")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R<IPage<CarouselImgVO>> list(@ApiIgnore @RequestParam Map<String, Object> carouselImg, Query query) {
		IPage<CarouselImgEntity> pages = carouselImgService.page(Condition.getPage(query), Condition.getQueryWrapper(carouselImg, CarouselImgEntity.class));
		return R.data(CarouselImgWrapper.build().pageVO(pages));
	}


	/**
	 * 首页轮播图表 新增
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入carouselImg")
	public R save(@Valid @RequestBody CarouselImgEntity carouselImg) {
		return R.status(carouselImgService.save(carouselImg));
	}

	/**
	 * 首页轮播图表 修改
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入carouselImg")
	public R update(@Valid @RequestBody CarouselImgEntity carouselImg) {
		return R.status(carouselImgService.updateById(carouselImg));
	}


	/**
	 * 首页轮播图表 删除
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(carouselImgService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 反馈的图片、视频文件上传
	 * @return ObjectStat
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@ServiceLog(value = TrainingOperateConstant.LIBRARY, operate = TrainingOperateConstant.ADD, describe = "轮播图上传")
	@SneakyThrows
	@ApiOperationSupport(order = 8)
	@PostMapping("/upload-file")
	@ApiOperation(value = "轮播图上传", notes = "轮播图上传")
	public R<String> questionPutFile(MultipartFile file) {
		String fileName =file.getOriginalFilename();
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
		MaterialLibraryEntity entity = materialLibraryService.buildAttach(fileName, file.getSize(), bladeFile,
			AttachType.CAROUSE_IMG.getCategory(), SecrecyLevel.NORMAL.getCode(),0);
		return R.data(entity.getStoragePath());
	}




	/**
	 * 导出数据
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/export-carouselImg")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "导出数据", notes = "传入carouselImg")
	public void exportCarouselImg(@ApiIgnore @RequestParam Map<String, Object> carouselImg, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<CarouselImgEntity> queryWrapper = Condition.getQueryWrapper(carouselImg, CarouselImgEntity.class);
		queryWrapper.lambda().eq(CarouselImgEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<CarouselImgExcel> list = carouselImgService.exportCarouselImg(queryWrapper);
		ExcelUtil.export(response, "首页轮播图表数据" + DateUtil.time(), "首页轮播图表数据表", list, CarouselImgExcel.class);
	}

}
